home *** CD-ROM | disk | FTP | other *** search
- Path: ludwig.esd.sgi.com!dscott
- From: dscott@ludwig.esd.sgi.com (Douglas Scott)
- Newsgroups: comp.lang.c++
- Subject: Forcing instantiation of static methods in template classes
- Date: 3 Jan 1996 22:23:06 GMT
- Organization: Silicon Graphics, Inc., Mountain View, CA
- Message-ID: <4cevka$ct3@fido.asd.sgi.com>
- NNTP-Posting-Host: ludwig.esd.sgi.com
-
- I have a series of template classes which all contain a static member
- function, rather like this:
-
- class FooBase {
- };
-
- template <class T>
- class Foo : public FooBase {
- public:
- Foo() {}
- static FooBase* create();
- };
-
- template <class T>
- FooBase* Foo<T>::create() { return new Foo<T>; }
-
-
- I assumed that if I created a static struct which included a pointer to each
- static member function that I wished to have instantiated:
-
- typedef FooBase* (*FooFun)();
-
- FooFun funs[] = {
- &Foo<char>::create, &Foo<short>::create, &Foo<int>::create
- };
-
-
- ... that the compiler would be forced to instantiate the template classes
- Foo<char>, Foo<short>, and Foo<int>, respectively. Upon compiling the file
- containing the template function declarations and definitions plus these
- static arrays of pointers-to-static-member-functions, and then linking that
- object against another file to generate an executable, I get linker errors
- indicating that, in effect, none of these static functions are being created,
- because their symbols are not found. Here is the beginning of the actual
- error messages from the real source:
-
- ld:
- Unresolved:
- ChannelChange<float>::create(const char*,int,int,const PCMMap&,double*,unsigned int,void*,int*)
- ChannelChange<double>::create(const char*,int,int,const PCMMap&,double*,unsigned int,void*,int*)
- ChannelChange<int>::create(const char*,int,int,const PCMMap&,double*,unsigned int,void*,int*)
- Swap<int>::create(const char*,void*,int*)
- Swap<float>::create(const char*,void*,int*)
- ...
- etc.
-
-
- Am I making an invalid assumption? Or is this a compiler-dependent behavior?
- Given that I have a finite and determined number of template class
- instantiations, is there another way to force the instantiation of them?
-
- Thanks.
-
- --
- ---
- Douglas Scott | Silicon Graphics, Inc.
- dscott@esd.sgi.com | Digital Media Systems
- (415) 933-6404 Fax: (415) 390-6159 | http://reality.sgi.com/employees/dscott
-